home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / U-Z / VideoToolBox Folder / VideoToolboxSources / kbhit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-16  |  1.1 KB  |  52 lines  |  [TEXT/KAHL]

  1. /*
  2. kbhit.c
  3. 6/13/90    dgp wrote it, based on suggestion from Michael Kahl and an earlier version by
  4.         Evan Relkin.
  5. 2/16/93    dgp    added YesOrNo().
  6. */
  7. #ifndef THINK_C
  8.     #error "Sorry, kbhit.c requires THINK C."
  9. #endif
  10. #include "VideoToolbox.h"
  11. #include <console.h>
  12.  
  13. int kbhit(void)
  14. {
  15.     int c;
  16.  
  17.     c = getcharUnbuffered();
  18.     if (c == EOF) return 0;
  19.     ungetc(c, stdin);
  20.     return 1;
  21. }
  22.  
  23. int getcharUnbuffered(void)
  24. {
  25.     int c;
  26.  
  27.     csetmode(C_RAW,stdin);        /* unbuffered, no echo, no editing */
  28.     c = getchar();
  29.     csetmode(C_ECHO,stdin);        /* default mode: line-buffered, echo, full editing */
  30.     return c;
  31. }
  32.  
  33. // Accept one character yes/no answer and spell out "Yes" or "No"
  34. Boolean YesOrNo(Boolean defaultAnswer)
  35. {
  36.     char c;
  37.     Boolean answer;
  38.     
  39.     if(defaultAnswer)printf(" (Yes):");
  40.     else printf(" (No):");
  41.     fflush(stdout);
  42.     do{
  43.         c=getcharUnbuffered();
  44.     }while(c==-1);
  45.     if(c==14)abort();                            // Crude test for command-period.
  46.     if(defaultAnswer)answer=!(tolower(c)=='n');    // Anything but 'n' or 'N' means yes
  47.     else answer=(tolower(c)=='y');                // Anything but 'y' or 'Y' means no
  48.     if(answer)printf("Yes.");
  49.     else printf("No.");
  50.     return answer;
  51. }
  52.